from urllib import parse

import pymysql
import requests
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QLabel, QInputDialog, QMessageBox, QLineEdit)
from PyQt5.QtGui import QPixmap, QPalette, QBrush
from PyQt5.QtCore import Qt
import sys
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUi()
    def initUi(self):
        self.setObjectName("Form")
        self.setFixedSize(993, 574)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("tim.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.setWindowIcon(icon)
        self.label = QtWidgets.QLabel(self)
        self.label.setGeometry(QtCore.QRect(60, 130, 81, 41))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self)
        self.pushButton.setGeometry(QtCore.QRect(160, 290, 101, 41))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(self)
        self.lineEdit.setGeometry(QtCore.QRect(170, 130, 181, 41))
        self.lineEdit.setObjectName("lineEdit")
        self.radioButton = QtWidgets.QRadioButton(self)
        self.radioButton.setGeometry(QtCore.QRect(20, 220, 151, 19))
        self.radioButton.setObjectName("radioButton")
        self.radioButton_2 = QtWidgets.QRadioButton(self)
        self.radioButton_2.setGeometry(QtCore.QRect(170, 220, 115, 19))
        self.radioButton_2.setObjectName("radioButton_2")
        self.radioButton_3 = QtWidgets.QRadioButton(self)
        self.radioButton_3.setGeometry(QtCore.QRect(290, 220, 115, 19))
        self.radioButton_3.setObjectName("radioButton_3")
        self.pushButton_2 = QtWidgets.QPushButton(self)
        self.pushButton_2.setGeometry(QtCore.QRect(160, 370, 101, 41))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self)
        self.pushButton_3.setGeometry(QtCore.QRect(160, 450, 101, 41))
        self.pushButton_3.setObjectName("pushButton_2")
        self.label_2 = QtWidgets.QLabel(self)
        self.label_2.setGeometry(QtCore.QRect(470, 140, 111, 16))
        self.label_2.setObjectName("label_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(self)
        self.lineEdit_2.setGeometry(QtCore.QRect(600, 130, 181, 41))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.textBrowser = QtWidgets.QTextBrowser(self)
        self.textBrowser.setGeometry(QtCore.QRect(530, 260, 256, 192))
        self.textBrowser.setObjectName("textBrowser")
        # pixmap = QPixmap("2.jpg").scaled(self.size())  # 适应窗口大小
        # palette = QPalette()
        # palette.setBrush(QPalette.Window, QBrush(pixmap))
        # self.setPalette(palette)
        self.retranslateUi(self)
        palette = QPalette()
        palette.setColor(QPalette.WindowText, Qt.red)  # 设置字体颜色
        self.label_2.setPalette(palette)
        self.label.setPalette(palette)
        self.textBrowser.setText("\t   通知\n     优酷视频暂时播放不了 两种解析方式 第一种 复制网页url(全网vip视频) 在点击播放 第二种 输入电影查询 查询过后播放提供了三种方式、\n\n     虽然软件gui界面长的丑 但是功能齐全 \n     定期更新数据库")
        self.show()
        self.vip_url = "http://app.baiyug.cn:2019/vip/index.php?url="
        self.infomation = "暂时没写数据库"
        self.radioButton.setChecked(True)
        self.website = ""


    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "坑坑视频解析器"))
        self.label.setText(_translate("Form", "电影的名字"))
        self.pushButton.setText(_translate("Form", "搜索"))
        self.radioButton.setText(_translate("Form", "爱奇艺"))
        self.radioButton_2.setText(_translate("Form", "优酷"))
        self.radioButton_3.setText(_translate("Form", "腾讯视频"))
        self.pushButton_2.setText(_translate("Form", "观看"))
        self.pushButton_3.setText(_translate("Form", "退出"))
        self.label_2.setText(_translate("Form", "电影url地址"))
        self.show()
        self.pushButton.clicked.connect(self.showDialog1)
        self.pushButton_2.clicked.connect(self.showDialog0)
        self.radioButton.clicked.connect(self.checkDialog)
        self.radioButton_2.clicked.connect(self.checkDialog)
        self.radioButton_3.clicked.connect(self.checkDialog)
        self.pushButton_3.clicked.connect(self.closeEvent)
        # self.setWindowFlags(Qt.FramelessWindowHint)#可以取消窗口

        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(20)
        self.pushButton.setFont(font)
        self.pushButton_2.setFont(font)
        self.pushButton_3.setFont(font)
        font = QtGui.QFont()
        font.setFamily("微软雅黑 Light")
        font.setPointSize(12)
        self.label.setFont(font)
        self.label_2.setFont(font)
        self.label.setObjectName("label")



    def showDialog0(self):
        # 播放
        self.text1 = self.lineEdit_2.displayText()
        if self.text1:
            QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.text1))
            self.lineEdit_2.setText("")
            self.textBrowser.setText("")
        else:
            QMessageBox.warning(self, '真香警告', '大哥请输入电影名字或者网址', QMessageBox.Yes | QMessageBox.No)

    def showDialog1(self):
        # 查找
        self.text = str(self.lineEdit.displayText())
        self.textBrowser.setText("")
        self.lineEdit_2.setText("")
        if self.text:
            # #从数据库里面找 找到就是yes 没找到就在弹出框
            self.connectDataBase()
            reply = QMessageBox.information(self, "查询数据库", self.infomation, QMessageBox.Yes | QMessageBox.No)
            if reply == QtWidgets.QMessageBox.Yes and self.infomation == "已找到该电影":
                QMessageBox.information(self, "提示信息", "请点击播放", QMessageBox.Yes)
            elif reply == QtWidgets.QMessageBox.Yes and self.infomation == "未找到该电影":
                QMessageBox.information(self, "提示信息", "未找到该电影请尝试输入网址", QMessageBox.Yes)
            else:
                QMessageBox.warning(self, '真香警告', '拜拜嘞 您', QMessageBox.Yes | QMessageBox.No)

        else:
            QMessageBox.warning(self, '真香警告', '大哥请输入电影名字', QMessageBox.Yes | QMessageBox.No)

    def connectDataBase(self):
        self.text = self.lineEdit.displayText()
        conn = pymysql.connect(host='172.16.67.12', user='root', passwd="13882", db='movies')
        cur = conn.cursor()
        try:
            cur.execute(
                "SELECT movie_url,content FROM {0} WHERE movie_name='{1}'".format(self.checkDialog(), self.text))
            print("SELECT movie_url,content FROM {0} WHERE movie_name='{1}'".format(self.checkDialog(), self.text))
            result = cur.fetchone()

            if len(result):
                if self.text == "王钰琴":
                    self.lineEdit_2.setText(result[0])
                    self.infomation = "我爱你 王钰琴"
                else:
                    self.lineEdit_2.setText(self.littelURL(self.vip_url + result[0]))
                    self.infomation = "已找到该电影"
                self.textBrowser.append(result[1])
        except:
            self.infomation = "未找到该电影"
        finally:
            self.lineEdit.setText("")
            cur.close()
            conn.close()

    def littelURL(self, reuslt):
        # 短视频接口
        data = parse.urlencode({
            "url": reuslt
        })
        # url = "http://mrw.so/api.php?" + data
        url = "http://h5ip.cn/index/api?" + data
        response = requests.get(url=url)
        return response.text

    def checkDialog(self):
        if self.radioButton_2.isChecked():
            self.website = "youku"
        elif self.radioButton_3.isChecked():
            self.website = "tencent"
        else:
            self.website = "aiqiyi"
        return self.website


    def closeEvent(self, event):
        reply = QtWidgets.QMessageBox.question(self, '本程序', "是否要退出程序？",
                                               QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
                                               QtWidgets.QMessageBox.No)
        if reply == QtWidgets.QMessageBox.Yes:
            event.accept()
            # reply = QMessageBox.warning(self, '真香警告', '想出去就出去哦，回答我一个问题', QMessageBox.Yes)
            # if reply == QMessageBox.Yes:
            #     text, ok = QInputDialog.getText(self, "退出程序", '输入我的名字')
            #     if ok:
            #         if text == "坑坑":
            #             event.accept()
            #         else:
            #             QMessageBox.warning(self, "我的名字", "我名字错了 是否给你机会",
            #                                 QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
            #
            #     text = QInputDialog.getText(self, "退出程序", '输入李佳隆是 -> 大丑逼')
            #     if text == "大丑逼":
            #         event.accept()
            #     else:
            #         QtGui.QDesktopServices.openUrl(QtCore.QUrl("https://www.tfswufe.edu.cn"))
            #         QMessageBox.information(self, "核查中", "不想填哇 \n那你出不去了 \n 算了再给你一次机会",
            #                                 QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
            #         text1, ok = QInputDialog.getText(self, "再次输入", '输入李佳隆是 -> 大丑逼')
            #         if ok:
            #             if text1 == "大丑逼":
            #                 event.accept()
            #             else:
            #                 QMessageBox.ctitical(self, "再次核查中", "危险！即将打开未成年不该看视频！！！\n\n 要不然你考虑输入一下？。",
            #                                      QMessageBox.Yes | QMessageBox.No)
            #                 QtGui.QDesktopServices.openUrl(QtCore.QUrl(self.vip_url + "https://www.iqiyi.com/v_19rr8vlijs.html"))
            #                 text3, ok = QInputDialog.getText(self, "再次输入", '输入李佳隆是 -> 大丑逼')
            #                 if text3 == "大丑逼":
            #                     event.accept()
            #                 else:
            #                     QMessageBox.ctitical(self, "再次核查中", "危险！即将打开未成年不该看视频！！！\n\n 要不然你考虑输入一下？。",
            #                                          QMessageBox.Yes | QMessageBox.No)
            #                     QtGui.QDesktopServices.openUrl(
            #                         QtCore.QUrl(self.vip_url + "https://www.iqiyi.com/v_19rr8vlijs.html"))
            #
            #         else:
            #             QMessageBox.ctitical(self, "再次核查中", "危险！即将打开未成年不该看视频！！！\n\n 要不然你考虑输入一下？。",
            #                                  QMessageBox.Yes | QMessageBox.No)
            #             QtGui.QDesktopServices.openUrl(
            #                 QtCore.QUrl(self.vip_url + "https://www.iqiyi.com/v_19rr8vlijs.html"))
            #             event.ignore()
            #     QtGui.QDesktopServices.openUrl(QtCore.QUrl("https://www.tfswufe.edu.cn"))
            #     QMessageBox.information(self, "核查中", "不想填哇 \n那你出不去了 \n 算了再给你一次机会",
            #                             QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
            #     text1, ok = QInputDialog.getText(self, "再次输入", '输入李佳隆是 -> 大丑逼')

        else:
            event.ignore()


if  __name__=="__main__":
    import  sys
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
